﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Util;
using BLL;
using Model;

public partial class linkEdit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Util.UtilWeb.CheckRights(this);
            string strId = Request.QueryString["id"];
            LoadView();
            if (!string.IsNullOrWhiteSpace(strId))
                LoadView(int.Parse(strId));
        }
    }

    private void LoadView()
    {
        var lbl = Master.FindControl("lblTitle") as Label;
        lbl.Text = "链接管理";

    }

    private void LoadView(int id)
    {
        hdId.Value = id.ToString();
        btnAdd.Text = "修改";
        BllLink bll = new BllLink();
        var obj = bll.GetSingle(id);
        txtUrl.Text = obj.url;
        txtName.Text = obj.name;
        txtSort.Text = obj.sort.ToString();
        hdSmall.Value = obj.src;
        imgSmall.Src = "../" + hdSmall.Value;
        imgSmall.Visible = true;
        hdId.Value = obj.id.ToString();
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        string cmd = btnAdd.Text.Trim();
        BllLink bll = new BllLink();
        Blladdr bll1 = new Blladdr();
        string name = txtName.Text.Trim();
        string url = txtUrl.Text;
        string sort = txtSort.Text;
        if (name == "" || url == "" ||sort=="")
        {
            Util.UtilWeb.Message(this, Util.KoInfo.FillBlank, null);
            return;

        }

        if (cmd == "添加")
        {
            tb_link obj = new tb_link(); 
            obj.name = name;
            obj.src = hdSmall.Value;
            obj.name = name;
            obj.url = url;
            obj.targ = true;
            if (bll.Add(obj))
            {
                Response.Redirect("linkList.aspx");
            }
            else
            {
                Util.UtilWeb.Message(this, Util.KoInfo.OperateFailure, null);
            }
        }
        if (cmd == "修改")
        {
            int id = int.Parse(hdId.Value);
            tb_link obj = bll.GetSingle(id);
            obj.name = name;
            obj.src = hdSmall.Value;
            obj.name = name;
            obj.url = url;
            obj.targ = true;
            obj.sort = int.Parse(sort);
            
            if (bll.Update(obj) )
            {
                Response.Redirect("linkList.aspx");
            }
            else
            {
                Util.UtilWeb.Message(this, Util.KoInfo.OperateFailure, null);
            }
        }
    }

    protected void btnUpload1_Click(object sender, EventArgs e)
    {
        string path = Util.UtilWeb.UploadFile(this, fu, null, "../img/pic/");
        hdSmall.Value = path.Replace("../", "");
        imgSmall.Src = "../" + hdSmall.Value;
        imgSmall.Visible = true;
    }
}